<?php
/**
 * Created by ptmall
 * User : 河南邦耀网络科技有限公司
 * Date : 2020/9/10
 * Time : 16:09
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

class Address extends Api
{
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * @Explain :添加收货地址
     * @Date : 2020/9/10 14:42
     * @Author : 河南邦耀网络科技有限公司
     */
    public function add(){
        $params=$this->request->request();
        if(empty($params['mobile']) || empty($params['username']) || empty($params['address']))
            $this->error('参数不正确');
        $data=[
            'uid'=>$this->auth->id,
            'username'=>$params['username'],
            'mobile'=>$params['mobile'],
            'province'=>$params['province'],
            'city'=>$params['city'],
            'district'=>$params['district'],
            'address'=>$params['address'],
            'code'=>$params['code'],
            'isdefault'=>$params['isdefault'],
            'createtime'=>time(),
        ];
        if(Db::table('pt_address')->where(['uid'=>$data['uid']])->count()>20){
            $this->error('最多添加20个收货地址');
        }
        if($params['isdefault']==1){
            $result=Db::table('pt_address')->where(['uid'=>$this->auth->id])->update(['isdefault'=>0]);
        }
        $result=Db::table('pt_address')->insert($data);
        $this->success('添加成功');
    }

    /**
     * @Explain :更新
     * @Date : 2020/9/10 15:16
     * @Author : 河南邦耀网络科技有限公司
     */
    public function update(){
        $params=$this->request->request();
        if(empty($params['id']) || empty($params['mobile']) || empty($params['username'])) $this->error('参数错误');
        $data=[
            'username'=>$params['username'],
            'mobile'=>$params['mobile'],
            'province'=>$params['province'],
            'city'=>$params['city'],
            'district'=>$params['district'],
            'address'=>$params['address'],
            'code'=>$params['code'],
            'isdefault'=>$params['isdefault'],
        ];
        if($params['isdefault']==1){
            $result=Db::table('pt_address')->where(['uid'=>$this->auth->id])->update(['isdefault'=>0]);
        }
        $result=Db::table('pt_address')->where(['id'=>$params['id'],'uid'=>$this->auth->id])->update($data);
        $this->success('更新成功');
    }

    /**
     * @Explain :获取列表
     * @Date : 2020/9/10 15:24
     * @Author : 河南邦耀网络科技有限公司
     */
    public function getList(){
        $result=Db::table('pt_address')->where(['uid'=>$this->auth->id])->select();
        $this->success('ok',$result);
    }

    /**
     * @Explain :删除
     * @Date : 2020/9/10 15:16
     * @Author : 河南邦耀网络科技有限公司
     */
    public function remove(){
        $params=$this->request->request();
        if(empty($params['id'])) $this->error('参数错误');
        $result=Db::table('pt_address')->where(['id'=>$params['id'],'uid'=>$this->auth->id])->delete();
        $this->success('ok');
    }


}
